﻿using System;

namespace Unknown.Framework.Transform
{
    /// <summary>
    /// 文本处理类
    /// </summary>
    public static class TextHandler
    {
        /// <summary>
        /// 布尔值
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>结果</returns>
        public static bool Boolean(string value, bool defaultValue)
        {
            bool result = defaultValue;

            if (!string.IsNullOrEmpty(value))
            {
                bool parseValue = false;

                bool done = bool.TryParse(value, out parseValue);

                if (done)
                {
                    result = parseValue;
                }
            }

            return result;
        }

        /// <summary>
        /// 布尔值
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>结果</returns>
        public static bool? Boolean(string value, bool? defaultValue)
        {
            bool? result = defaultValue;

            if (!string.IsNullOrEmpty(value))
            {
                bool parseValue = false;

                bool done = bool.TryParse(value, out parseValue);

                if (done)
                {
                    result = parseValue;
                }
            }

            return result;
        }

        /// <summary>
        /// 十进制值
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>结果</returns>
        public static decimal Decimal(string value, decimal defaultValue)
        {
            decimal result = defaultValue;

            if (!string.IsNullOrEmpty(value))
            {
                decimal parseValue = 0;

                bool done = decimal.TryParse(value, out parseValue);

                if (done)
                {
                    result = parseValue;
                }
            }

            return result;
        }
        /// <summary>
        /// 十进制值
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>结果</returns>
        public static decimal? Decimal(string value, decimal? defaultValue)
        {
            decimal? result = defaultValue;

            if (!string.IsNullOrEmpty(value))
            {
                decimal parseValue = 0;

                bool done = decimal.TryParse(value, out parseValue);

                if (done)
                {
                    result = parseValue;
                }
            }

            return result;
        }

        /// <summary>
        /// 整型值
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>结果</returns>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "integer")]
        public static int Integer(string value, int defaultValue)
        {
            int result = defaultValue;

            if (!string.IsNullOrEmpty(value))
            {
                int parseValue = int.MinValue;

                bool done = int.TryParse(value, out parseValue);

                if (done)
                {
                    result = parseValue;
                }
            }

            return result;
        }

        /// <summary>
        /// 整型值
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>结果</returns>
        public static int? Integer(string value, int? defaultValue)
        {
            int? result = defaultValue;

            if (!string.IsNullOrEmpty(value))
            {
                int parseValue = int.MinValue;

                bool done = int.TryParse(value, out parseValue);

                if (done)
                {
                    result = parseValue;
                }
            }

            return result;
        }

        /// <summary>
        /// 整型值
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>结果</returns>
        public static long Number(string value, long defaultValue)
        {
            long result = defaultValue;

            if (!string.IsNullOrEmpty(value))
            {
                long parseValue = long.MinValue;

                bool done = long.TryParse(value, out parseValue);

                if (done)
                {
                    result = parseValue;
                }
            }

            return result;
        }

        /// <summary>
        /// 整型值
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>结果</returns>
        public static long? Number(string value, long? defaultValue)
        {
            long? result = defaultValue;

            if (!string.IsNullOrEmpty(value))
            {
                long parseValue = long.MinValue;

                bool done = long.TryParse(value, out parseValue);

                if (done)
                {
                    result = parseValue;
                }
            }

            return result;
        }

        /// <summary>
        /// 日期值
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>结果</returns>
        public static DateTime Time(string value, DateTime defaultValue)
        {
            DateTime result = defaultValue;

            if (!string.IsNullOrEmpty(value))
            {
                DateTime parseValue = DateTime.MinValue;

                bool done = DateTime.TryParse(value, out parseValue);

                if (done)
                {
                    result = parseValue;
                }
            }

            return result;
        }

        /// <summary>
        /// 日期值
        /// </summary>
        /// <param name="value">字符串</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>结果</returns>
        public static DateTime? Time(string value, DateTime? defaultValue)
        {
            DateTime? result = defaultValue;

            if (!string.IsNullOrEmpty(value))
            {
                DateTime parseValue = DateTime.MinValue;

                bool done = DateTime.TryParse(value, out parseValue);

                if (done)
                {
                    result = parseValue;
                }
            }

            return result;
        }
    }
}
